package com.ray.client;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import com.ray.basic.BASIC;

public class Udp_out implements Runnable{
	private String server=BASIC.get_server();
	private int servPort=BASIC.get_servPort();
	private int listenPort=BASIC.get_listenPort();
	public void run() {
		while(Client_in.get_clientid()==null){
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		DatagramSocket ds=null;
		DatagramPacket dp=null;
		try {
			ds = new DatagramSocket(listenPort);
			byte[] SingePkg=Client_in.get_clientid().getBytes();
			dp = new DatagramPacket(SingePkg, SingePkg.length, InetAddress.getByName(server), servPort);
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		for (int i=0; i<10; i++){
			try {
				ds.send(dp);
				Thread.sleep(10);
			} catch (IOException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		ds.close();
	}
}
